package hgx.spring.cloud.lession.productrepertory.controller;/**
 * Created by liujun on 2018/3/26.
 */

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import hgx.spring.cloud.lession.productrepertory.api.ProductRepository;
import hgx.spring.cloud.lession.productrepertory.vo.ProductVo;
import hgx.spring.cloud.lession.productrepertory.vo.ReponseProductListVo;
import hgx.spring.cloud.lession.productrepertory.vo.ReponseProductVo;

/**
 * @author Administrator
 * @create 2018-03-26 14:32
 */
@RestController
@RequestMapping("/products")
public class ProductController {
    private final static Logger logger=Logger.getLogger(ProductController.class);

    @Autowired
    private ProductRepository productRepository;

    /**
     * 保存商品信息
     * @param productVo
     * @return
     */
    @PostMapping("/save")
    public ReponseProductVo saveProduct(@RequestBody ProductVo productVo) {
        boolean flag = productRepository.createPorduct(productVo);
        ReponseProductVo reponse = new ReponseProductVo();
        if (flag) {
            reponse.setProductVo(productVo);
            reponse.success();
        }
        return reponse;
    }

    /**
     * 查询所有商品信息
     * @return
     */
    @GetMapping("")
    public ReponseProductListVo findAll(){
        ReponseProductListVo reponse=new ReponseProductListVo();
        List<ProductVo> list=productRepository.findAll();
        if(list !=null && !list.isEmpty()){
            reponse.setProductVos(list);
            reponse.success();
        }

        return reponse;
    }

    /**
     * 根据商品ID 查询商品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ReponseProductVo getProductById(@PathVariable("id") Long id){
        ReponseProductVo reponse=new ReponseProductVo();
        ProductVo productVo=productRepository.getProductById(id);
        if(productVo !=null){
            reponse.setProductVo(productVo);
            reponse.success();
        }

        return reponse;
    }

    @PostMapping("/ids")
    public ReponseProductListVo getProductByIds(@RequestBody IDRequest idRequest){
        ReponseProductListVo reponse=new ReponseProductListVo();
        List<ProductVo> productVos=productRepository.findProductByIds(idRequest.getIds());
        if(productVos !=null){
            reponse.setProductVos(productVos);
            reponse.success();
        }

        return reponse;
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity<Exception> handleException(HttpServletRequest request, Exception e)
    {
//        logger.error("Request FAILD, URL = {} method = {}", request.getRequestURI().toString(), request.getMethod().toString());
        logger.error(e.toString(), e);
        return new ResponseEntity<Exception>(e, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    public static final class IDRequest {
        List<Long> ids;

        public List<Long> getIds() {
            return ids;
        }

        public void setIds(List<Long> ids) {
            this.ids = ids;
        }
    }

}
